package com.example.controller;

import cn.hutool.cron.CronUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.bean.CronDefinition;
import com.example.config.dynamicTask.CronTaskRegistrar;
import com.example.task.TaskRunnableSwitch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.config.CronTask;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZhengDing
 * @date 2022/4/9 21:13
 */
@RestController
@Slf4j
public class TestController {

    @Autowired
    TaskScheduler taskScheduler;
    
    @Autowired
    CronTaskRegistrar cronTaskRegistrar;
    
    @GetMapping("add")
    public Object add(Integer id, Integer step) {
        CronDefinition cronDefinition = new CronDefinition(id, "0/" + step + " * * * * ?", "test");
        
        cronTaskRegistrar.addCronTask(cronDefinition.getCronId(), new TaskRunnableSwitch(cronDefinition), cronDefinition.getCronExpression());
        
        return cronDefinition;
    }
    
    @GetMapping("delete")
    public Object delete(Integer id) {
        
        cronTaskRegistrar.removeCronTask(id);
        
        return "success";
    }
    
    private boolean checkCronExpression(String expression) {
        // CronUtil.getScheduler()
        
        return false;
    }
}
